package csel.view;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import csel.model.*;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;

/*
 * Reasons for Existence: 
 * Team Members Responsible
 * Implementation: Alex, Andrew
 * Testing: Alex, Andrew
 */
public class EquipmentViewer extends JPanel implements Observer
{
	ButtonGroup itemButtonGroup;
	LinkedList<JToggleButton> itemButtons;
	JButton unequip;

	JPanel buttonPane;
	JPanel itemsPane;
	
	public EquipmentViewer(Entity e, ActionListener a)
	{
		e.addObserver(this);
		
		buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane,BoxLayout.X_AXIS));
		
		unequip = new JButton("Unequip");
		unequip.setActionCommand("stunequip");
		unequip.addActionListener(a);
		modelUpdate(e);
	}

	public EquippableItem[] getSelectedItems()
	{
		LinkedList<EquippableItem> selectedItems = new LinkedList<EquippableItem>();
		int size = itemButtons.size();
		for (int a = 0; a < size; ++a)
		{
			if (itemButtons.get(a).getSelectedObjects() != null)
				selectedItems.add((EquippableItem) itemButtons.get(a)
						.getClientProperty("myItem"));
		}
		if (size != 0)
		{
			return (EquippableItem[]) selectedItems.toArray();
		} else
		{
			return null;
		}
	}
	
	public void setEntity(Entity e)
	{
		e.addObserver(this);
		modelUpdate(e);
	}
	
	public void modelUpdate(Object o)
	{
		removeAll();
		
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		buttonPane.add(unequip);
		
		Entity entity = ((Entity) o);
		Dictionary<String,EquippableItem> eItems = entity.getEquippedItems();
		int size = eItems.size();
		
		itemsPane = new JPanel();
		itemsPane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		int rowNum = 0;
		
		EquippableItem[] eList = new EquippableItem[size];
		int a = 0;
		for (Enumeration<EquippableItem> eItemsEnumeration = eItems.elements() ; eItemsEnumeration.hasMoreElements() ;) {
			eList[a] = (EquippableItem) eItemsEnumeration.nextElement();
			++a;
	    }
		itemButtonGroup = new ButtonGroup();
		itemButtons = new LinkedList<JToggleButton>();
		for (int b = 0; b < size; ++b)
		{
			JToggleButton j = new JToggleButton(new ImageIcon(RepresentationFactory.getInstance().get(eList[b]).getSimpleImage()));
			j.putClientProperty("myItem", eList[b]);
			j.setPreferredSize(new Dimension(50,50));
			itemButtonGroup.add(j);
			itemButtons.add(j);
			c.gridx = 0;
			c.gridy = rowNum;
			itemsPane.add(j,c);
			c.gridx = 1;
			c.gridy = rowNum++;
			itemsPane.add(new JLabel(eList[b].getDescription()),c);
		}
		
		buttonPane.setOpaque(false);
		itemsPane.setOpaque(false);
		
		add(buttonPane);
		add(itemsPane);
		
		revalidate();
        //repaint();
	}
	
}
